package com.xin.di.uav.applet.shop.controller;

import com.xin.di.uav.applet.mapper.CommodityMapper;
import com.xin.di.uav.applet.resp.ChangeShoppingCartResponse;
import com.xin.di.uav.applet.resp.ShoppingCartResponse;
import com.xin.di.uav.applet.rest.CommodityRest;
import com.xin.di.uav.common.awt.JwtUtil;
import com.xin.di.uav.common.enums.ServicePrefixEnum;
import com.xin.di.uav.common.request.common.IdRequest;
import com.xin.di.uav.common.utils.RedisPrefixKey;
import com.xin.di.uav.common.utils.Rs;
import com.xin.di.uav.user.order.po.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author 01430312
 */
@RestController
@RequestMapping("/sys/shopping-cart/")
public class ShoppingCartController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private CommodityMapper commodityMapper;

    @Autowired
    private CommodityRest commodityRest;

    @PostMapping("add-one")
    public Rs<ChangeShoppingCartResponse> addShoppingCart(@RequestBody IdRequest idRequest) {
        Product commodity = commodityRest.getById(idRequest.getId());
        if (ObjectUtils.isEmpty(commodity)) {
            throw new RuntimeException("商品不存在");
        }
        int amount = commodity.getStock();
        if (amount < 1) {
            throw new RuntimeException("库存不足");
        }
        ChangeShoppingCartResponse changeShoppingCartResponse = new ChangeShoppingCartResponse();
        String userId = JwtUtil.getUserId();
        HashMap obj = (HashMap) redisTemplate.opsForValue().get(ServicePrefixEnum.RESCUE + RedisPrefixKey.SHOPPING_CART + userId);
        HashMap hashMap = new HashMap();
        if (!ObjectUtils.isEmpty(obj)) {
            //购物车不为空
            hashMap = obj;
            Integer integer = (Integer) hashMap.get(idRequest.getId());
            if (!ObjectUtils.isEmpty(integer)) {
                //如果这件商品有记录
                hashMap.put(idRequest.getId(), integer + 1);
            } else {
                //如果这件商品没有记录
                hashMap.put(idRequest.getId(), 1);
            }
        } else {
            //购物车为空
            hashMap.put(idRequest.getId(), 1);
        }
        redisTemplate.opsForValue().set(ServicePrefixEnum.RESCUE + RedisPrefixKey.SHOPPING_CART + userId, hashMap);
        changeShoppingCartResponse.setPrice(commodity.getSalePrice());
        return Rs.success(changeShoppingCartResponse);
    }

    @PostMapping("sub-one")
    public Rs<ChangeShoppingCartResponse> subShoppingCart(@RequestBody IdRequest idRequest) {
        String id = idRequest.getId();
        ChangeShoppingCartResponse changeShoppingCartResponse = new ChangeShoppingCartResponse();
        Product commodity = commodityRest.getById(id);
        if (ObjectUtils.isEmpty(commodity)) {
            throw new RuntimeException("商品不存在");
        }
        String userId = JwtUtil.getUserId();
        HashMap obj = (HashMap) redisTemplate.opsForValue().get(ServicePrefixEnum.RESCUE + RedisPrefixKey.SHOPPING_CART + userId);
        HashMap hashMap = new HashMap();
        if (!ObjectUtils.isEmpty(obj)) {
            //购物车不为空
            hashMap = obj;
            Integer integer = (Integer) hashMap.get(id);
            if (ObjectUtils.isEmpty(integer) || integer == 0) {
                throw new RuntimeException("商品数量为0");
            }
            if (integer == 1) {
                hashMap.remove(id);
            } else {
                hashMap.put(id, integer - 1);
            }
        }
        redisTemplate.opsForValue().set(ServicePrefixEnum.RESCUE + RedisPrefixKey.SHOPPING_CART + userId, hashMap);
        changeShoppingCartResponse.setPrice(commodity.getSalePrice());
        return Rs.success(changeShoppingCartResponse);
    }

    @GetMapping("get")
    public Rs<List<ShoppingCartResponse>> getShoppingCart() {
        List<ShoppingCartResponse> shoppingCartResponses = new ArrayList<>();
        String userId = JwtUtil.getUserId();
        HashMap obj = (HashMap) redisTemplate.opsForValue().get(ServicePrefixEnum.RESCUE + RedisPrefixKey.SHOPPING_CART + userId);
        if (!ObjectUtils.isEmpty(obj)) {
            obj.forEach((o, o2) -> {
                ShoppingCartResponse shoppingCartResponse = new ShoppingCartResponse();
                shoppingCartResponse.setId((String) o);
                shoppingCartResponse.setCnt((Integer) o2);
                shoppingCartResponses.add(shoppingCartResponse);
            });
        }
        return Rs.success(shoppingCartResponses);
    }
}
